<html>
	<head>
	<script src="js/jQuery.js"></script>
	<script src="js/hash.js"></script>
	<script type="text/javascript">
	var port;

	var api_key = "0bcccfa21e04d9cbb01dfe498a8203e1";
	var secret = "9269b3937233ffaa37a0ab91e8739702";
  
  chrome.extension.onConnect.addListener(function(p) {
	  port = p;

	  port.onMessage.addListener(function(msg) {
	  	now_playing(msg);
	  });
	});
  
  chrome.extension.onRequest.addListener(
	function(request, sender, sendResponse) {
		if(request.action == "login") { //They just got an session key
			localStorage["sk"] = request.key; //Save session key

			sendResponse({}); //send blank response

			login();
		} else if (request.action == "need_login") {
			if(localStorage["sk"] != null) { //no session key stored
				sendResponse({"need":false});

				login();
			} else {
				sendResponse({"need":true});
			}
		}

	   
	});

	var isLoggedIn = false;
	var currentTrackName = "";
	var currentArtistName = "";
	var currentAlbumName = "";
	var currentDuration = 0;
	var timeSongStarted = 0;
	var isScrobbled = false;
	var wasPaused = false;
				
	function login() {
		isLoggedIn = true;

		check_music();
	}

	function check_music() {
		port.postMessage({});

		setTimeout(check_music,1000); //check again in 2000 ms
	}

	function scrobble() {
		if(currentTrackName == "") return;

		isScrobbled = true;
		
		var api_sig = md5("api_key" + api_key + "artist" + currentArtistName + "methodtrack.scrobblesk" + localStorage["sk"] + 
				"timestamp" + timeSongStarted + "track" + currentTrackName + secret);
		
		xmlhttp2=new XMLHttpRequest();
		xmlhttp2.onreadystatechange=function()
		  {
		  if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
		    {
		    //alert(xmlhttp2.responseText);
		    }
		  }
		xmlhttp2.open("POST","http://ws.audioscrobbler.com/2.0/",true);
		xmlhttp2.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
		xmlhttp2.send("method=track.scrobble&track=" + encodeURIComponent(currentTrackName) + "&artist=" + encodeURIComponent(currentArtistName) + "&api_key=" + api_key + 
				"&timestamp=" + timeSongStarted + "&api_sig=" + api_sig + "&sk=" + localStorage["sk"]);
		}

	function now_playing(msg) {
		var playTime = Math.round(new Date().getTime()/1000) - timeSongStarted;

		// see if we need to scrobble
		if((playTime>240 || playTime>(msg.duration/2)) && msg.duration>30 && !isScrobbled && currentTrackName != "") {
			scrobble(); //need to scrobble the track
		}
		
		if(currentTrackName != msg.trackName && msg.duration != 0) {
			
			currentTrackName = msg.trackName;
			currentArtistName = msg.artistName;
			currentAlbumName = msg.albumName;
			timeSongStarted = Math.round(new Date().getTime()/1000);
			isScrobbled = false;
			
			if(currentTrackName == "") return;

			// build api signature for now playing submission
			var api_sig = md5("api_key" + api_key + "artist" + msg.artistName + "duration" + msg.duration + 
					"methodtrack.updateNowPlayingsk" + localStorage["sk"] + 
					"track" + msg.trackName + secret);
			
			xmlhttp=new XMLHttpRequest();
			xmlhttp.onreadystatechange=function()
			  {
			  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			    {
			   //alert(xmlhttp.responseText);
			    }
			  }
			  
			xmlhttp.open("POST","http://ws.audioscrobbler.com/2.0/",true);
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
			xmlhttp.send("method=track.updateNowPlaying&track=" + encodeURIComponent(msg.trackName) + "&artist=" + encodeURIComponent(msg.artistName) + "&api_key=" + api_key + 
					"&api_sig=" + api_sig + "&sk=" + localStorage["sk"] + "&duration=" + msg.duration);
		}
	}
	
	</script>
  </head>
  <body>
  Google Music Scrobbler
  </body>
</html>